package com.example.deepschool.Controller.Course;


import com.example.deepschool.Model.Course;
import com.example.deepschool.Model.Response.ApiResponse;
import com.example.deepschool.enums.GlobalVariables_String;
import com.example.deepschool.service.composite.CompositeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "课程API")
@RestController("Course")
@RequestMapping("/Course")
public class Controller {
	
	// 用户令牌的全局变量
	private final String TOKEN = GlobalVariables_String.TOKEN.getValue();
	
	@Operation(summary = "添加课程",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`"
	)
	@PostMapping("/addCourse")
	public ResponseEntity<Object> addCourse (@Validated @RequestBody Course course, HttpServletRequest request) {
		
		String UUID = request.getHeader(TOKEN);
		CompositeService.course.addCourse(course, UUID);
		Map<String, Object> map = new HashMap<>();
		map.put("msg", "课程添加成功");
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "更新课程信息",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@PostMapping("/update")
	public ResponseEntity<Object> update (@Validated @RequestBody Course course, HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		Course newCourse = CompositeService.course.update(course, UUID);
		Map<String, Object> map = new HashMap<>();
		map.put("data", newCourse);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "上传课程封面",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@PostMapping("/putCoverImage")
	public ResponseEntity<Object> putCoverImage (@RequestParam MultipartFile CoverImage,
	                                             @RequestParam String fileSHA256,
	                                             @RequestParam String courseId,
	                                             HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		Course course = CompositeService.course.putCoverImage(CoverImage, fileSHA256, courseId, UUID);
		Map<String, Object> map = new HashMap<>();
		map.put("data", course);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "获取课程封面")
	@GetMapping("/Not_verified/getCoverImage")
	public ResponseEntity<ResourceRegion> getCoverImage (@RequestParam String CoverImage, HttpServletRequest request) {
		String Range = request.getHeader(HttpHeaders.RANGE);
		return ApiResponse.GetApiResponse()
				       .binBody(CompositeService.course.getCourseImage(CoverImage, Range))
				       .onlineBuild();
	}
	
	@Operation(summary = "获取课程列表")
	@GetMapping("/getCourseByPagination")
	public ResponseEntity<Object> getCourseByPagination (@RequestParam Integer Page,
	                                                     @RequestParam Integer limit) {
		Course[] courses = CompositeService.course.getCourseByPagination(Page, limit);
		Map<String, Object> map = new HashMap<>();
		map.put("data", courses);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "根据课程名称获取课程列表")
	@GetMapping("/getCourseByName")
	public ResponseEntity<Object> getCourseByName (@RequestParam String name) {
		Course[] courses = CompositeService.course.getCourseByName(name);
		Map<String, Object> map = new HashMap<>();
		map.put("data", courses);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "获取用户管理的课程列表",
	           description = "**注意**：此API需要在请求头添加认证Token,字段名为 `Token`")
	@GetMapping("/CourseByUserId")
	public ResponseEntity<Object> CourseByUserId (HttpServletRequest request) {
		String UUID = request.getHeader(TOKEN);
		Course[] courses = CompositeService.course.getCourseByUserId(UUID);
		Map<String, Object> map = new HashMap<>();
		map.put("data", courses);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
	
	@Operation(summary = "根据课程ID获取课程")
	@GetMapping("/getCourse")
	public ResponseEntity<Object> getCourse (@RequestParam Long courseId) {
		Course course = CompositeService.course.getCourse(courseId);
		Map<String, Object> map = new HashMap<>();
		map.put("data", course);
		return ApiResponse.GetApiResponse()
				       .ok()
				       .body(map)
				       .build();
	}
}
